package com.app.OOS.controller;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.app.OOS.domain.Batch_Coursewares;
import com.app.OOS.domain.Score;
import com.app.OOS.domain.User;
import com.app.OOS.domain.User_courseware_batch;
import com.app.OOS.service.UserService;

@Controller
public class UserController {
	@Autowired
	UserService userService;
	//注册
	@ResponseBody
	@RequestMapping("/insertUser")
	public  Map<String, Object> insertUser(int userId,String userName,String password) {
		userService.insertUser(userId, userName, password);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("message", "注册待审核");
		return map;
	}
	
	//登陆
	@ResponseBody
	@RequestMapping("/selectUser")
	public  Map<String, String> selectUser(int userId,String password,int type,HttpSession session,HttpServletRequest request) {
		User user = new User();
		user.setUserId(userId);
		user.setPassword(password);
		user.setType(type);
		Map<String, String> map = new HashMap<String, String>();
		User user2 = userService.selectUser(user);
		if(type==1) {
			if(user2==null) {
				map.put("msg", "错误");  
		        return map;
			}else if(user2.getSetting()==1) {
				HttpSession httpSession=request.getSession();
	    		
	    		httpSession.setAttribute("userId", user2.getUserId());
	    		httpSession.setAttribute("userPassword", user2.getPassword());
			   
				map.put("msg", "系统管理员");
			}else  if(user2.getSetting()==0) {
				map.put("msg", "管理员账号待激活");
			}else {
				map.put("msg", "系统错误1");
			}
		}else if(type==0) {
			if(user2==null) {
				map.put("msg", "错误");  
		        return map;
			}else if(user2.getSetting()==1) {
				HttpSession httpSession=request.getSession();
	    		httpSession.setAttribute("userId", user2.getUserId());
	    		httpSession.setAttribute("userPassword", user2.getPassword());
				map.put("msg", "学员");
			}else if(user2.getSetting()==0) {
				map.put("msg", "审核");
			}
		}else {
			map.put("msg", "系统错误2");
		}
		return map;
	}
	
	//修改密码
	@ResponseBody
	@RequestMapping("/updatePassword")
	public  Map<String, Object> updatePassword(int userId,String repw) {
		User user = new User();
		user.setUserId(userId);
		user.setPassword(repw);
		userService.updatePassword(user);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("message", "修改信息");
		return map;
		
	}
	
	//查询学员-批次-课件
	@ResponseBody
	@RequestMapping("/selectBacthNameCoursewareName")
	public  Map<String, Object> selectBacthNameCoursewareName(HttpServletRequest request,int page,int limit) {
	int	tmp=(page-1)*limit;
	HttpSession httpSession=request.getSession();
	List<Batch_Coursewares> list = userService.selectBacthNameCoursewareName(httpSession.getAttribute("userId"),tmp,limit);
	int tol=userService.tol(httpSession.getAttribute("userId"));
	Map<String, Object> map = new HashMap<String, Object>();
	map.put("list", list);
	map.put("message", "信息");
	map.put("tol", tol);
	map.put("status", 200);
	return map;
	}
	
	//查找视频src
	@ResponseBody
	@RequestMapping("/selectSrc")
	public  Map<String, Object> selectSrc(int coursewareId) {
		String src = userService.selectSrc(coursewareId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("src", src);
		System.out.println(src);
		return map;
	}
	
	//查看成绩
	
		@ResponseBody
		@RequestMapping("/selectScore22")
		public  Map<String, Object>  selectScore1(HttpServletRequest request) {
			HttpSession httpSession=request.getSession();
			List<Score> list = userService.selectScore1(httpSession.getAttribute("userId"));
			System.out.println("............:"+list);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("list", list);
			map.put("message", "信息");
			map.put("tol", 1);
			map.put("status", 200);
			return map;
		}
	
		//查看学习记录
		
			@ResponseBody
			@RequestMapping("/selecRecod")
			public  Map<String, Object>  selecRecod(HttpServletRequest request) {
				HttpSession httpSession=request.getSession();
				List<User_courseware_batch> list = userService.selectRecord(httpSession.getAttribute("userId"));
				System.out.println("............:"+list);
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("list", list);
				map.put("message", "信息");
				map.put("tol", 1);
				map.put("status", 200);
				return map;
			}
	//插入记录
			@ResponseBody
			@RequestMapping("/insertStudy")
			public  Map<String, Object> insertUser(HttpServletRequest request,int coursewareId,int batchId) {
				HttpSession httpSession=request.getSession();
				Map<String, Object> map = new HashMap<String, Object>();
				map=userService.insertStudy(httpSession.getAttribute("userId"), coursewareId, batchId);
				return map;
			}
	//更新时间	
		
			@ResponseBody
			@RequestMapping("/updateTime")
			public  Map<String, Object> updateTime(HttpServletRequest request,int coursewareId,int batchId,int time) {
				HttpSession httpSession=request.getSession();
				Map<String, Object> map = new HashMap<String, Object>();
				userService.updatetime(httpSession.getAttribute("userId"), coursewareId, batchId,time);
				map.put("message", "yes");
				return map;
}
			
			
			
			
			//更新状态
			
			@ResponseBody
			@RequestMapping("/updateState")
			public  Map<String, Object> updateState(HttpServletRequest request,int coursewareId,int batchId) {
				HttpSession httpSession=request.getSession();
				Map<String, Object> map = new HashMap<String, Object>();
				userService.updateState(httpSession.getAttribute("userId"), coursewareId, batchId);
				map.put("message", "yes");
				return map;
}
			
			
			//参加考试
			
			@ResponseBody
			@RequestMapping("/doEx")
			public  Map<String, Object> doEx(HttpServletRequest request) {
				HttpSession httpSession=request.getSession();
				Map<String, Object> map = new HashMap<String, Object>();
				String s=	userService.doExamal(httpSession.getAttribute("userId"));
				map.put("message",s);
				return map;
}
			
			
			
			
}